package cn.xshi.route.web;

import cn.xshi.route.service.DynamicRouteModulesService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.base.BaseSearch;
import cn.xshi.common.util.StringUtil;
import cn.xshi.route.model.DynamicRouteModules;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 路由模块
 * 2015-05-24 08:36:53  邓纯杰
 */
@RestController
@RequestMapping("/dynamicRouteModules")
@Api(value = "路由模块",tags = "路由模块",description = "路由模块")
public class DynamicRouteModulesController extends BaseAction {

    @Resource
    DynamicRouteModulesService dynamicRouteModulesService;

    /**
     * 查询路由模块列表并分页
     * @param baseSearch
     */
    @PostMapping(value="/list")
    @Auth(value = "/dynamicRouteModules/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    @ApiOperation(value="查询路由模块列表并分页", notes="查询路由模块列表并分页")
    public BasePage<List<DynamicRouteModules>> getDynamicRouteModulesListByCondition(@RequestBody(required=true)BaseSearch baseSearch){
        Map<String, Object> condition = baseSearch.convert();
        commonHPager(baseSearch);
        List<DynamicRouteModules> dynamicRouteModulesList = dynamicRouteModulesService.getDynamicRouteModulesListByCondition(condition);
        PageInfo<DynamicRouteModules> page = new PageInfo<DynamicRouteModules>(dynamicRouteModulesList);
        return outPageBootStr(page,baseSearch);
    }

    /**
     * 查询单个路由模块
     * @param id
     */
    @GetMapping(value="/get/{id}")
    @Auth(value = "/dynamicRouteModules/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    @ApiOperation(value="查询单个路由模块", notes="查询单个路由模块")
    public BaseResult<DynamicRouteModules> getDynamicRouteModulesById(@PathVariable("id")String id){
        DynamicRouteModules dynamicRouteModules = dynamicRouteModulesService.getDynamicRouteModulesById(id);
        return BaseResult.success(dynamicRouteModules);
    }

    /**
     * 添加
     * @param dynamicRouteModules
     * @return
     */
    @PostMapping(value="/add")
    @Auth("/dynamicRouteModules/add")
    @ApiOperation(value="添加", notes="添加")
    public BaseResult addDynamicRouteModules(@RequestBody DynamicRouteModules dynamicRouteModules){
        int i = 0;
        if(null != dynamicRouteModules){
            i=dynamicRouteModulesService.addDynamicRouteModules(dynamicRouteModules);
        }
        if(i>0){
            return BaseResult.success();
        }else{
            return BaseResult.fail();
        }
    }

    /**
     * 编辑
     * @param dynamicRouteModules
     */
    @PutMapping(value="/update")
    @Auth("/dynamicRouteModules/update")
    @ApiOperation(value="编辑", notes="编辑")
    public BaseResult updateDynamicRouteModules(@RequestBody DynamicRouteModules dynamicRouteModules){
        int i = 0;
        if(null !=dynamicRouteModules){
            i=dynamicRouteModulesService.updateDynamicRouteModules(dynamicRouteModules);
        }
        if(i>0){
            return BaseResult.success();
        }else{
            return BaseResult.fail();
        }
    }

    /**
     * 删除
     * @param id
     */
    @DeleteMapping(value="/delete")
    @Auth("/dynamicRouteModules/delete")
    @ApiOperation(value="删除", notes="删除")
    public BaseResult delDynamicRouteModules(String id){
        int i = 0;
        if(!StringUtil.isEmpty(id)){
            Map<String, Object> condition = new HashMap<String, Object>();
            condition.put("id",id.split(","));
            i=dynamicRouteModulesService.delDynamicRouteModules(condition);
        }
        if(i>0){
            return BaseResult.success();
        }else{
            return BaseResult.fail();
        }
    }

    /**
     * 查询路由模块列表
     */
    @GetMapping(value="/listAll")
    @Auth(value = "/dynamicRouteModules/listAll",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    @ApiOperation(value="查询路由模块列表", notes="查询路由模块列表")
    public BaseResult<List<DynamicRouteModules>> getDynamicRouteModulesList(){
        Map<String,Object> condition = new HashMap<>();
        List<DynamicRouteModules> dynamicRouteModulesList = dynamicRouteModulesService.getDynamicRouteModulesListByCondition(condition);
        return BaseResult.success(dynamicRouteModulesList);
    }
}
